package com.cx.controller;

import com.cx.pojo.Department;
import com.cx.pojo.DepartmentPages;
import com.cx.service.impl.DepartmentServiceImpl;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(value = "部门的API",description="部门相关的CRUD功能")
public class DepartmentServlet {

    @Autowired
    private DepartmentServiceImpl departmentServiceImpl;

//    @GetMapping("/{pageNum}")
//    @ApiOperation(value = "通过pageNum查询某页数据" )
//    public PageInfo<Department> findAll(@PathVariable int pageNum){
//
//        PageInfo<Department> page = departmentServiceImpl.findAll(pageNum);
//
//        List<Department> departmentList = page.getList();
//
//        return page;
//
//    }

    @PostMapping("/query")
    @ApiOperation(value = "通过pageNum和sn联合查询某页数据" )
    public PageInfo<Department> queryfindAll(@RequestBody DepartmentPages departmentPages){

        PageInfo<Department> page = departmentServiceImpl.findAll(departmentPages);

        List<Department> departmentList = page.getList();

        return page;

    }

    @PostMapping("/add")
    @ApiOperation(value = "通过department类型添加数据" )
    public void addDept(@RequestBody Department department){
        if (department.getId() == null){

            departmentServiceImpl.addDept(department);
        }else {
            departmentServiceImpl.updateDept(department);
        }
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "通过ID删除" )
    public void delDept(@PathVariable int id){
        departmentServiceImpl.deleteById(id);
    }

    @PutMapping("/update")
    @ApiOperation(value = "通过department类型修改数据" )
    public void updateDept(@RequestBody Department department){
        departmentServiceImpl.updateDept(department);
    }



    @PatchMapping
    @ApiOperation(value = "通过id数组批量删除数据" )
    public void patchDel(@RequestBody List<Integer> ids){
        departmentServiceImpl.patchDel(ids);
    }

    @GetMapping("/getDepts")
    @ApiOperation(value = "获取所有上级部门数据" )
    public List<Department> getDeptTree(){
        return departmentServiceImpl.getDeptTree();
    }

}
